//
//  DDLoginUserManager.m
//  DingDongZP
//
//  Created by 王威 on 16/3/23.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDLoginUserManager.h"

static NSString *const kFirstLauchApp = @"kFirstLauchApp";
static NSString *const kCurrentAppVersion = @"kCurrentAppVersion";


static NSString *const kUserInfoUserID = @"kUserInfoUserID";
static NSString *const kUserInfoCommonUserID = @"kUserInfoCommonUserID";

static NSString *const kUserInfoNickname = @"kUserInfoNickname";
static NSString *const kUserInfoAvatar = @"kUserInfoAvatar";
static NSString *const kUserInfoEmail = @"kUserInfoEmail";

static NSString *const kUserAccount = @"kUserAccount";
static NSString *const kUserPassword = @"kUserPassword";
static NSString *const kUserToken = @"kUserToken";
static NSString *const kUserName = @"kUserName";
static NSString *const kUserNamefirst = @"kUserNamefirst";
static NSString *const kUserPortrait = @"kUserPortrait";


@implementation DDLoginUserManager

//static NSString *const kFirstLauchApp = @"kFirstLauchApp";
//static NSString *const kCurrentAppVersion = @"kCurrentAppVersion";
//
//
//static NSString *const kUserInfoUserID = @"kUserInfoUserID";
//static NSString *const kUserInfoCommonUserID = @"kUserInfoCommonUserID";
//
//static NSString *const kUserInfoNickname = @"kUserInfoNickname";
//static NSString *const kUserInfoAvatar = @"kUserInfoAvatar";
//static NSString *const kUserInfoEmail = @"kUserInfoEmail";
//
//static NSString *const kUserAccount = @"kUserAccount";
//static NSString *const kUserPassword = @"kUserPassword";
//static NSString *const kUserToken = @"kUserToken";

#pragma mark - User Base info

//暂时版本更新 当成 第一次登录处理
+ (BOOL)isFirstLaunch
{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *newApp_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSString *app_Version = [self objectFromUserDefaultsKey:kCurrentAppVersion];
    
    //第一次安装
    if (newApp_Version == nil) {
        
        [self saveToUserDefaultsObject:newApp_Version forKey:kCurrentAppVersion];
        return  true;
        
    } else {
        
        //版本一致
        if ([app_Version isEqualToString:newApp_Version]) {
            
            return false;
        } else {
            //版本升级
            [[LDCacheManager shareInstance] cleanAllCache];
            [self saveToUserDefaultsObject:newApp_Version forKey:kCurrentAppVersion];
            return true;
        }
    }
    
//    return [self boolFromUserDefaultsKey:kFirstLauchApp];
}
//+ (BOOL)isUpdateVersionLaunch {
//    
//    NSString *vv = [self objectFromUserDefaultsKey:kCurrentAppVersion];
//    
//    return 0;
//}

+ (void)setFistLaunch:(BOOL)yesorno
{
    [self saveToUserDefaultsBOOL:yesorno forKey:kFirstLauchApp];
}

+ (BOOL)isLogin
{
    if (([self token] != nil) && ([self account] != nil)) {
        
        return true;
    } else {
        
        return false;
    }
}

+ (void)clearLoginInfo {
    
    [self setToken:nil];
    [self setUserID:nil];
}


+ (NSString *)account
{

    return [self objectFromUserDefaultsKey:kUserAccount];
}
+ (void)setAccount:(NSString *)account
{
    [self saveToUserDefaultsObject:account forKey:kUserAccount];
}

+ (NSString *)password
{

    
    return [self objectFromUserDefaultsKey:kUserPassword];
}
+ (void)setPassword:(NSString *)password
{
    [self saveToUserDefaultsObject:password forKey:kUserPassword];
}

+ (NSString *)userID {
    return [self objectFromUserDefaultsKey:kUserInfoUserID];
}

+ (void)setUserID:(NSString *)userID {
    [self saveToUserDefaultsObject:userID forKey:kUserInfoUserID];
}

+ (NSString *)nickname {
    return [self objectFromKeyChainKey:kUserInfoNickname];
}

+ (void)setNickname:(NSString *)nickname{
    [self saveToKeyChainObject:nickname forKey:kUserInfoNickname];
}

+ (NSString *)avatar {
    return [self objectFromUserDefaultsKey:kUserInfoAvatar];
}

+ (void)setAvatar:(NSString *)avatar {
    [self saveToUserDefaultsObject:avatar forKey:kUserInfoAvatar];
}

+ (NSString *)email {
    return [self objectFromUserDefaultsKey:kUserInfoEmail];
}

+ (void)setEmail:(NSString *)email {
    [self saveToUserDefaultsObject:email forKey:kUserInfoEmail];
}

+ (NSString *)token
{
    return [self objectFromUserDefaultsKey:kUserToken];
}
+ (void)setToken:(NSString *)token
{
    [self saveToUserDefaultsObject:token forKey:kUserToken];
}

// 用户姓名
+ (NSString *)accountName
{
    return [self objectFromUserDefaultsKey:kUserName];
}
+ (void)setAccountName:(NSString *)accountName
{
    [self saveToUserDefaultsObject:accountName forKey:kUserName];
}

// 用户姓名登录跟Token一起获取
+ (NSString *)accountNamefirst
{
    return [self objectFromUserDefaultsKey:kUserNamefirst];
}

+ (void)setAccountNamefirst:(NSString *)accountNamefirst
{
    [self saveToUserDefaultsObject:accountNamefirst forKey:kUserNamefirst];
}
//用户头像
+ (NSString *)ddportrait
{

    return [self objectFromUserDefaultsKey:kUserPortrait];
}
+ (void)setDdportrait:(NSString *)ddportrait
{
  
    [self saveToUserDefaultsObject:ddportrait forKey:kUserPortrait];
}
#pragma mark - Common Method

+ (id)objectFromUserDefaultsKey:(NSString *)key {
    
    
//    BOOL x = [[NSUserDefaults standardUserDefaults] objectIsForcedForKey:key];
    return [[NSUserDefaults standardUserDefaults] objectForKey:key];
}

+ (void)saveToUserDefaultsObject:(id)object forKey:(NSString *)key {
    
    if (object && ![object isKindOfClass:[NSNull class]]) {
        
        [[NSUserDefaults standardUserDefaults] setObject:object forKey:key];
    } else {
        
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
    }
    
    [[NSUserDefaults standardUserDefaults] synchronize];
}


+ (NSInteger)integerFromUserDefaultsKey:(NSString *)key {
    return [[NSUserDefaults standardUserDefaults] integerForKey:key];
}

+ (void)saveToUserDefaultsInteger:(NSInteger)intValue forKey:(NSString *)key {
    [[NSUserDefaults standardUserDefaults] setInteger:intValue forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (BOOL)boolFromUserDefaultsKey:(NSString *)key {
    return [[NSUserDefaults standardUserDefaults] boolForKey:key];
}

+ (void)saveToUserDefaultsBOOL:(BOOL)boolValue forKey:(NSString *)key {
    [[NSUserDefaults standardUserDefaults] setBool:boolValue forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (void)saveToKeyChainObject:(id)object forKey:(NSString *)key {
    
    if (object && ![object isKindOfClass:[NSNull class]]) {
        [[NSUserDefaults standardUserDefaults] setObject:object forKey:key];
    } else {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
    }
    
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (id)objectFromKeyChainKey:(NSString *)key {
    return [[NSUserDefaults standardUserDefaults] objectForKey:key];
}

+ (NSString *)weixin {
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"weixin"];
}

+ (void)setWeixin:(NSString *)weixin {
    [[NSUserDefaults standardUserDefaults] setObject:weixin forKey:@"weixin"];
}

+ (NSString *)phone {
    return [self account];
    return  [[NSUserDefaults standardUserDefaults]  objectForKey:@"phone"];
}

+ (void)setPhone:(NSString *)phone {
    [self setAccount:phone];
    [[NSUserDefaults standardUserDefaults] setObject:phone
                                              forKey:@"phone"];
}

@end
